{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Common Words With One Occurrence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countWords"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计出现过一次的公共字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个字符串数组&nbsp;<code>words1</code>&nbsp;和&nbsp;<code>words2</code>&nbsp;，请你返回在两个字符串数组中 <strong>都恰好出现一次</strong>&nbsp;的字符串的数目。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>words1 = [\"leetcode\",\"is\",\"amazing\",\"as\",\"is\"], words2 = [\"amazing\",\"leetcode\",\"is\"]\n",
    "<b>输出：</b>2\n",
    "<strong>解释：</strong>\n",
    "- \"leetcode\" 在两个数组中都恰好出现一次，计入答案。\n",
    "- \"amazing\" 在两个数组中都恰好出现一次，计入答案。\n",
    "- \"is\" 在两个数组中都出现过，但在 words1 中出现了 2 次，不计入答案。\n",
    "- \"as\" 在 words1 中出现了一次，但是在 words2 中没有出现过，不计入答案。\n",
    "所以，有 2 个字符串在两个数组中都恰好出现了一次。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>words1 = [\"b\",\"bb\",\"bbb\"], words2 = [\"a\",\"aa\",\"aaa\"]\n",
    "<b>输出：</b>0\n",
    "<b>解释：</b>没有字符串在两个数组中都恰好出现一次。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>words1 = [\"a\",\"ab\"], words2 = [\"a\",\"a\",\"a\",\"ab\"]\n",
    "<b>输出：</b>1\n",
    "<b>解释：</b>唯一在两个数组中都出现一次的字符串是 \"ab\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= words1.length, words2.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= words1[i].length, words2[j].length &lt;= 30</code></li>\n",
    "\t<li><code>words1[i]</code> 和&nbsp;<code>words2[j]</code>&nbsp;都只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-common-words-with-one-occurrence](https://leetcode.cn/problems/count-common-words-with-one-occurrence/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-common-words-with-one-occurrence](https://leetcode.cn/problems/count-common-words-with-one-occurrence/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"leetcode\",\"is\",\"amazing\",\"as\",\"is\"]\\n[\"amazing\",\"leetcode\",\"is\"]', '[\"b\",\"bb\",\"bbb\"]\\n[\"a\",\"aa\",\"aaa\"]', '[\"a\",\"ab\"]\\n[\"a\",\"a\",\"a\",\"ab\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        a=[key for key,value in Counter(words1).items() if value==1]\n",
    "        b=[key for key,value in Counter(words2).items() if value==1]\n",
    "        return len(set(a) & set(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        a=Counter(words1)\n",
    "        b=Counter(words2)\n",
    "        s=0\n",
    "        for i in a.keys():\n",
    "            if a[i]==1 and b[i]==1:\n",
    "                s+=1\n",
    "        return s\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        ans=0\n",
    "        for i in words1:\n",
    "            if words1.count(i)==1 and words2.count(i)==1:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        word1_map = Counter(words1)\n",
    "        word2_map = Counter(words2)\n",
    "        return sum(word2_map[word] == 1  for word in word1_map if word1_map[word] == 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        words1=collections.Counter(words1)\n",
    "        words2=collections.Counter(words2)\n",
    "        return sum(i in words2 and words1[i]==1 and words2[i]==1 for i in words1.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        res = 0\n",
    "        for i in words1:\n",
    "            if words1.count(i) == 1 and words2.count(i) == 1:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        from collections import Counter\n",
    "\n",
    "        words1 = dict(Counter(words1))\n",
    "        words2 = dict(Counter(words2))\n",
    "\n",
    "        res = 0\n",
    "        for word in words1:\n",
    "            if word in words2 and words1[word] == 1 and words2[word] == 1:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        freq1 = Counter(words1)\n",
    "        freq2 = Counter(words2)\n",
    "        ans = 0\n",
    "        for word in freq1.keys():\n",
    "            if freq1[word] == 1 and freq2[word] == 1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        res = 0\n",
    "        for i in words1:\n",
    "            if i in words2 and words1.count(i) == 1 and  words2.count(i) == 1:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        num=0\n",
    "        dict1 = {k:v for k,v in collections.Counter(words1).items() if v==1}\n",
    "        dict2 = {k:v for k,v in collections.Counter(words2).items() if v==1}\n",
    "        for i in dict1.keys():\n",
    "            if dict2.get(i)==1:\n",
    "                num+=1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        mydic1=collections.Counter(words1)\n",
    "        mydic2=collections.Counter(words2)\n",
    "        ans=0\n",
    "        for k,v in mydic1.items():\n",
    "            if k in mydic2.keys() and mydic1[k]==1 and mydic2[k]==1:\n",
    "                ans=ans+1\n",
    "        return ans\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        c1, c2 = Counter(words1), Counter(words2)\n",
    "        ret = 0\n",
    "        for k, v in c1.items():\n",
    "            if 1 == v and v == c2.get(k, 0):\n",
    "                ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        w1,w2 = collections.Counter(words1),collections.Counter(words2)\n",
    "        return sum([1 for i in w1.keys() if w1[i] == 1 and w2[i] == 1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        w1 = Counter(words1)\n",
    "        w2 = Counter(words2)\n",
    "        res = 0\n",
    "        for k,v in w1.items():\n",
    "            if v ==1:\n",
    "                if w2.get(k,'') == 1:\n",
    "                    res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        new_words = []\n",
    "        for i in range(len(words1)):\n",
    "            a = True\n",
    "            for j in range(0, len(words1)):\n",
    "                if words1[i] == words1[j] and i !=j:\n",
    "                    a = False\n",
    "            if a == True:\n",
    "                new_words.append(words1[i])\n",
    "        new_words_2 = []\n",
    "        for i in range(len(words2)):\n",
    "            a = True\n",
    "            for j in range(0, len(words2)):\n",
    "                if words2[i] == words2[j] and i != j:\n",
    "                    a = False\n",
    "            if a == True:\n",
    "                new_words_2.append(words2[i])\n",
    "        total_count = 0\n",
    "        for i in new_words:\n",
    "            count = 0\n",
    "            for j in new_words_2:\n",
    "                if i == j:\n",
    "                    count += 1\n",
    "            if count == 1:\n",
    "                total_count += 1\n",
    "        return total_count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        freq1 = Counter(words1)\n",
    "        freq2 = Counter(words2)\n",
    "        ans = 0\n",
    "        for w, val in freq1.items():\n",
    "            if val == 1 and freq2.get(w) == 1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        freq1 = Counter(words1)\n",
    "        freq2 = Counter(words2)\n",
    "        res = 0\n",
    "        for word1 in freq1.keys():\n",
    "            if freq1[word1] == 1 and freq2[word1] == 1:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        counter1 = Counter(words1)\n",
    "        counter2 = Counter(words2)\n",
    "        one_occurrence_strings = set(word for word, count in counter1.items() if count == 1 and counter2.get(word, 0) == 1)\n",
    "        one_occurrence_strings.update(word for word, count in counter2.items() if count == 1 and counter1.get(word, 0) == 1)\n",
    "\n",
    "        return len(one_occurrence_strings)\n",
    "       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        a=0\n",
    "        for i in words1:\n",
    "            if i in words2:\n",
    "                if words1.count(i)==1 and words2.count(i)==1:\n",
    "                    a+=1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        count = 0\n",
    "        for data in set(words1):\n",
    "            if data in words2 and words1.count(data) == words2.count(data) == 1:\n",
    "                count += 1\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        freq1 = Counter(words1)\n",
    "        freq2 = Counter(words2)\n",
    "        res = 0\n",
    "        for word1 in freq1.keys():\n",
    "            if freq1[word1]==1 and freq2[word1]==1:\n",
    "                res +=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        hax = Counter(words1)\n",
    "        ha = Counter(words2)\n",
    "        return len({x for x in hax if hax[x] == 1 and ha[x] == 1})\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        c1 = Counter(words1)\n",
    "        c2 = Counter(words2)\n",
    "\n",
    "        res = 0\n",
    "        for i in words1:\n",
    "            if c1[i] == 1 and c2[i] == 1:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        count = dict()\n",
    "        for word in words1:\n",
    "            if word in count:\n",
    "                count[word] = 3\n",
    "            else:\n",
    "                count[word] = 1\n",
    "        \n",
    "        for word in words2:\n",
    "            if word in count:\n",
    "                count[word] += 1\n",
    "\n",
    "        return len([k for k,v in count.items() if v==2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        dic_1 = {}\n",
    "        dic_2 = {}\n",
    "        for i in words1:\n",
    "            dic_1[i] = dic_1.get(i, 0) + 1\n",
    "        for i in words2:\n",
    "            dic_2[i] = dic_2.get(i, 0) + 1\n",
    "        return len(set(dict(filter(lambda l:l[1] == 1, dic_1.items())).keys()) & set(dict(filter(lambda l:l[1] == 1, dic_2.items())).keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        freq1 = Counter(words1)\n",
    "        freq2 = Counter(words2)\n",
    "        res=0\n",
    "        for words in freq1.keys():\n",
    "            if freq1[words]==1 and freq2[words]==1:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        sum = 0\n",
    "        for i in words1 :\n",
    "            if words1.count(i) == 1 and words2.count(i) == 1 :\n",
    "                sum+=1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        c1=Counter(words1)\n",
    "        c2=Counter(words2)\n",
    "        ans=0\n",
    "        for k in set(c1.keys())|set(c2.keys()):\n",
    "            if c1[k]==c2[k]==1:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        count = dict()\n",
    "        for word in words1:\n",
    "            if word in count:\n",
    "                count[word] = 3\n",
    "            else:\n",
    "                count[word] = 1\n",
    "        \n",
    "        for word in words2:\n",
    "            if word in count:\n",
    "                count[word] += 1\n",
    "\n",
    "        return Counter(count.values())[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        cnt1 = Counter(words1)\n",
    "        cnt2 = Counter(words2)\n",
    "        res = 0\n",
    "        for k, v in cnt1.items():\n",
    "            if v == 1 and cnt2[k] == 1:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        dic1 = Counter(words1)\n",
    "        dic2 = Counter(words2)\n",
    "        lst1 = set([k for k, v in dic1.items() if v == 1])\n",
    "        lst2 = set([k for k, v in dic2.items() if v == 1])\n",
    "        return len(lst1 & lst2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        word_dict1 = Counter(words1)\n",
    "        word_dict2 = Counter(words2)\n",
    "\n",
    "        result = 0\n",
    "        for key, value in word_dict1.items():\n",
    "            if value == 1 and key in word_dict2 and  word_dict2[key] == 1:\n",
    "                result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        a=set(words1).intersection(set(words2))\n",
    "        b=0\n",
    "        for i in a:\n",
    "            if words1.count(i) == words2.count(i)==1:\n",
    "                b+=1\n",
    "        return b\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        words1 = set(i[0] for i in filter(lambda x: x[1]==1,Counter(words1).most_common()))\n",
    "        words2 = set(i[0] for i in filter(lambda x: x[1]==1,Counter(words2).most_common()))\n",
    "        return len(words1 & words2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        count1, count2, count = {}, {}, 0\n",
    "        for word in words1:\n",
    "            count1[word] = count1.get(word, 0) + 1\n",
    "        for word in words2:\n",
    "            count2[word] = count2.get(word, 0) + 1\n",
    "        for word in count1:\n",
    "            if count1[word] == 1 and count2.get(word, 0) == 1:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        c1 = Counter(words1)\n",
    "        c2 = Counter(words2)\n",
    "        return sum(c2[k] == 1 for k, v in c1.items() if v == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        g1, g2 = Counter(words1), Counter(words2)\n",
    "        ans = 0\n",
    "        for k in g1.keys():\n",
    "            if g1[k] == 1 and g2.get(k, 0) == 1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        ans=0\n",
    "        for word in words1:\n",
    "            if words1.count(word)==1 and words2.count(word)==1:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        count=0\n",
    "        for i in words1:\n",
    "            if words1.count(i)==1 and words2.count(i)==1:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        dic1 = collections.Counter(words1)\n",
    "        dic2 = collections.Counter(words2)\n",
    "        res = 0\n",
    "        for word in dic1:\n",
    "            if dic1[word]==1 and dic2[word]==1:\n",
    "                res += 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        word_dict1 = Counter(words1)\n",
    "        word_dict2 = Counter(words2)\n",
    "\n",
    "        result = 0\n",
    "        for key, value in word_dict1.items():\n",
    "            if value == 1 and key in word_dict2 and  word_dict2[key] == 1:\n",
    "                result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        count = 0\n",
    "        for i in words1:\n",
    "            if words1.count(i) == 1 and words2.count(i) == 1:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        freq1 = Counter(words1)   # words1 中字符串的出现次数\n",
    "        freq2 = Counter(words2)   # words2 中字符串的出现次数\n",
    "        res = 0   # 出现过一次的公共字符串个数\n",
    "        for word1 in freq1.keys():\n",
    "            # 遍历 words1 出现的字符并判断是否满足要求\n",
    "            if freq1[word1] == 1 and freq2[word1] == 1:\n",
    "                res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        ans = 0\n",
    "        dic1 = Counter(words1)\n",
    "        dic2 = Counter(words2)\n",
    "        for k in dic1:\n",
    "            if dic1[k] == 1 and dic2[k] == 1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        cw1 = Counter(words1)\n",
    "        cw2 = Counter(words2)\n",
    "        ans = 0\n",
    "        for k, v in cw1.items():\n",
    "            if cw1[k] == cw2[k] and cw1[k] == 1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        word1=Counter(words1)\n",
    "        word2=Counter(words2)\n",
    "        return sum(word2[word]==1 for word in word1 if word1[word]==1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        freq1 = Counter(words1)\n",
    "        freq2 = Counter(words2)\n",
    "        ans = 0\n",
    "        for word in words1:\n",
    "            if freq1[word] == 1 and freq2[word] == 1:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        uni_1 = set([_ for _ in words1 if words1.count(_) == 1])\n",
    "        uni_2 = set([_ for _ in words2 if words2.count(_) == 1])\n",
    "        uni = uni_1 & uni_2\n",
    "        return len(uni)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        d1 = collections.Counter(words1)\n",
    "        d2 = collections.Counter(words2)\n",
    "        r = 0\n",
    "        for k, v in d1.items():\n",
    "            if d2.get(k) == v == 1:\n",
    "                r += 1\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        freq1 = Counter(words1)   # words1 中字符串的出现次数\n",
    "        freq2 = Counter(words2)   # words2 中字符串的出现次数\n",
    "        res = 0   # 出现过一次的公共字符串个数\n",
    "        for word1 in freq1.keys():\n",
    "            # 遍历 words1 出现的字符并判断是否满足要求\n",
    "            if freq1[word1] == 1 and freq2[word1] == 1:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        a = {item[0] for item in Counter(words1).items() if item[1]== 1}\n",
    "        b = {item[0] for item in Counter(words2).items() if item[1] == 1}\n",
    "        return len(a & b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        res = []\n",
    "        ans = 0\n",
    "        for word in words1:\n",
    "            if words1.count(word)==1:res.append(word)\n",
    "        for word in res:\n",
    "            if words2.count(word)==1:ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        cnt=0\n",
    "        x=Counter(words1)\n",
    "        y=Counter(words2)\n",
    "        for i in y:\n",
    "            if x[i]==1 and y[i]==1: cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        res = 0\n",
    "        \n",
    "        wc1 = {}\n",
    "        for word in words1:\n",
    "            if word in wc1:\n",
    "                wc1[word] += 1\n",
    "            else:\n",
    "                wc1[word] = 1\n",
    "        \n",
    "        wc2 = {}\n",
    "        for word in words2:\n",
    "            if word in wc2:\n",
    "                wc2[word] += 1\n",
    "            else:\n",
    "                wc2[word] = 1\n",
    "        \n",
    "\n",
    "        for word in words2:\n",
    "            if wc1.get(word) == 1 and wc2.get(word) == 1:\n",
    "                res += 1\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        m = {}\n",
    "        for word in words1:\n",
    "            if word not in m.keys():\n",
    "                m[word] = 1\n",
    "            else:\n",
    "                m[word] += 1\n",
    "        m2 = {}\n",
    "        for word in words2:\n",
    "            if word not in m.keys():\n",
    "                continue\n",
    "            if word not in m2.keys():\n",
    "                m2[word] = 1\n",
    "            else:\n",
    "                m2[word] += 1\n",
    "        ans = 0\n",
    "        for key, cnt in m2.items():\n",
    "            if cnt != 1:\n",
    "                continue\n",
    "            if key not in m.keys():\n",
    "                continue\n",
    "            if m[key] != 1:\n",
    "                continue\n",
    "            ans += 1\n",
    "        return ans\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        count = 0\n",
    "        for word in words1:\n",
    "            if (words1.count(word) == 1) & (words2.count(word) == 1):\n",
    "                count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        combined_set = set(words1 + words2)\n",
    "\n",
    "        # 使用集合推导式过滤出在两个数组中都恰好出现一次的字符串\n",
    "        one_occurrence_strings = {word for word in combined_set if words1.count(word) == 1 and words2.count(word) == 1}\n",
    "\n",
    "        # 返回符合条件的字符串数目\n",
    "        return len(one_occurrence_strings)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        c1,c2 = Counter(words1),Counter(words2)\n",
    "        res = 0\n",
    "        for i in c1.keys():\n",
    "            if c1[i] == 1 and c2[i] == 1:res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        w1 = collections.Counter(words1)\n",
    "        w2 = collections.Counter(words2)\n",
    "\n",
    "        res = 0\n",
    "\n",
    "        for k1 in w1:\n",
    "            if w1[k1] == 1 and w2[k1] == 1:\n",
    "                res += 1\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        counter1 = Counter(words1)\n",
    "        counter2 = Counter(words2)\n",
    "        ans = 0\n",
    "        for k,v in counter1.items():\n",
    "            if v==1 and k in counter2 and counter2[k]==1:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "from collections import Counter\r\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\r\n",
    "        ans = 0\r\n",
    "        cnt1, cnt2 = Counter(words1), Counter(words2)\r\n",
    "        for c in cnt1 + cnt2:\r\n",
    "            if cnt1.get(c, 0) == 1 and cnt2.get(c, 0) == 1:\r\n",
    "                ans += 1\r\n",
    "        return ans\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        cnt1 = Counter(words1)\n",
    "        cnt2 = Counter(words2)\n",
    "        ans = 0\n",
    "        for w in cnt2:\n",
    "            ans += cnt1[w] == cnt2[w] == 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        d1 = collections.Counter(words1)\n",
    "        d2 = collections.Counter(words2)\n",
    "        r = 0\n",
    "        for k, v in d1.items():\n",
    "            if d2[k] == v == 1:\n",
    "                r += 1\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        c1,c2=Counter(words1),Counter(words2)\n",
    "        ans=0\n",
    "        for k,v in c1.items():\n",
    "            ans+=(c1[k]==1 and c2[k]==1)\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def countWords(self, words1: List[str], words2: List[str]) -> int:\n",
    "        counter_dict = Counter(words1)\n",
    "        counter_dict2 = Counter(words2)\n",
    "        res_words1 = {key for key, value in counter_dict.items() if value == 1}\n",
    "        res_words2 = {key for key, value in counter_dict2.items() if value == 1}\n",
    "        res = res_words1 & res_words2\n",
    "        return len(res)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
